package com.axin.common.security.handle;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONObject;
import com.axin.common.core.domain.web.AjaxResult;
import com.axin.common.core.utils.ServletUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * jwt 认证进入点 【认证失败处理类 返回未授权】
 *
 * @Author axin
 * @Date 2021/9/8 12:14
 **/
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {


        int code = HttpStatus.HTTP_UNAUTHORIZED;
        String msg = StrUtil.format("请求访问：{}，认证失败,无法访问系统资源，", request.getRequestURI());
        ServletUtils.renderString(response, JSONObject.toJSONString(AjaxResult.error(code, msg)));
    }
}
